﻿using Prism.Events;
using SPW.Infrastructure.Enums;
using SPW.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SyntacticSugar;
using SPW.Infrastructure.Models;
using TerraExplorerX;
using GeoJSON.Net.Feature;
using GeoJSON.Net.Contrib.Wkb;

namespace SPW.GenericDesktop.Codes
{
    /// <summary>
    /// skyline 常量配置
    /// </summary>
    public class SGWorldConfig
    {
        /// <summary>
        /// 默认标记图片
        /// </summary>
        public const string DefaultMarkerIcon = "Images\\marker\\0_24.png";
        /// <summary>
        /// 默认线条色
        /// </summary>
        public const uint DefaultLineColor = 0xFF00FF00;
        /// <summary>
        /// 默认填充色
        /// </summary>
        public const uint DefaultFillColor = 0x77FFEE00;
        public const double DefaultLineWidth = -2;
    }

    /// <summary>
    /// skyline 管理
    /// </summary>
    public class SGWorldManager : ISGWorld
    {
        private TerraExplorerX.ISGWorld701 _SGWorld;
        private readonly IEventAggregator _eventAggregator;
        private readonly Infrastructure.Events.EarthRenderEvent _earthRenderEvent;
        private IReaderOperating readerOperating;
        private ISGWorldInteractionOperating interactionOperating;

        /// <summary>
        /// 是否渲染完成
        /// </summary>
        public bool IsReaderCompleted { get; private set; }

        public SGWorldManager(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            //获取事件对象
            _earthRenderEvent = _eventAggregator?.GetEvent<Infrastructure.Events.EarthRenderEvent>();
        }

        public IReaderOperating Reader()
        {
            //初始化内部接口
            _SGWorld = new TerraExplorerX.SGWorld701();
            //初始化处理事件
            if (_SGWorld is TerraExplorerX._ISGWorld701Events_Event viewerEvents)
            {
                viewerEvents.OnLoadFinished += ViewerEvents_OnLoadFinished;
                viewerEvents.OnSGWorldMessage += ViewerEvents_OnSGWorldMessage;
                viewerEvents.OnFrame += ViewerEvents_OnFrame;
                viewerEvents.OnLButtonDown += ViewerEvents_OnLButtonDown;
                viewerEvents.OnRButtonDown += ViewerEvents_OnRButtonDown;
            }
            if (!IsReaderCompleted) readerOperating = new ReaderOperating(_SGWorld);
            IsReaderCompleted = true;//标记为渲染完成
            return readerOperating;
        }

        #region Skyline Events
        private void ViewerEvents_OnLoadFinished(bool bSuccess)
        {
            //发送消息
            _earthRenderEvent?.Publish(new Infrastructure.Events.EarthRenderEvent(bSuccess));
        }

        private bool ViewerEvents_OnRButtonDown(int Flags, int X, int Y)
        {
            bool result = true;
            if (interactionOperating?.IsDispose == false)
            {
                var r = interactionOperating?.RightButtonDown(Flags, X, Y);
                result = r.Value;
            }

            return result;
        }

        private bool ViewerEvents_OnLButtonDown(int Flags, int X, int Y)
        {
            bool result = false;
            if (interactionOperating?.IsDispose == false)
            {
                var r = interactionOperating?.LeftButtonDown(Flags, X, Y);
                result = r.Value;
            }

            return result;
        }

        private void ViewerEvents_OnFrame()
        {
            if (interactionOperating?.IsDispose == false)
            {
                interactionOperating?.Frame();
            }
        }

        private bool ViewerEvents_OnSGWorldMessage(string MessageID, string SourceObjectID)
        {
            return true;
        }
        #endregion

        public void FlyRoam(double x, double y, double distance, double pitch)
        {
            throw new NotImplementedException();
        }

        public IDrawingOperating Drawing(DrawingMode mode, Action<DrawResult, IDrawingOperating> callback)
        {
            //回收资源
            interactionOperating?.Dispose();
            switch (mode)
            {
                case DrawingMode.Point:
                    {
                        interactionOperating = new PointDrawingHandle(_SGWorld, callback);
                    }
                    break;
                case DrawingMode.Polyline:
                    {
                        interactionOperating = new LineDrawingHandle(_SGWorld, callback);
                    }
                    break;
                case DrawingMode.Polygon:
                    {
                        interactionOperating = new PolygonDrawingHandle(_SGWorld, callback);
                    }
                    break;
            }
            return interactionOperating as IDrawingOperating;
        }

        public void CloseDrawing()
        {
            interactionOperating?.Dispose();
            //恢复鼠标模式
            _SGWorld.Window.SetInputMode(0);
        }

        public void Pick(Action<IGeometry> callback)
        {
            interactionOperating = new PickFeatureHandle(_SGWorld, callback);
        }
    }

    /// <summary>
    /// 渲染操作
    /// </summary>
    public class ReaderOperating : IReaderOperating
    {
        private readonly string spaceDbConnStringFormat;
        private readonly TerraExplorerX.ISGWorld701 _SGWorld;

        public ReaderOperating(TerraExplorerX.ISGWorld701 sGWorld)
        {
            _SGWorld = sGWorld;
            spaceDbConnStringFormat = "OGRConnectionString={ PG:{0}Tables={1} };TEPlugName=OGR;";
        }

        public IFeatureOperating OpenLayer(string displayName, string tableName, string connString)
        {
            //_SGWorld.Command.Execute(1013, 0);
            //拼接字符串
            var createFeatureLayerConnString = string.Format(spaceDbConnStringFormat, connString, tableName);
            var feature = _SGWorld.Creator.CreateFeatureLayer(displayName, createFeatureLayerConnString);
            //TODO:图层设置
            feature.Load();

            return new FeatureOperating();
        }

        public bool OpenModel(string path)
        {
            throw new NotImplementedException();
        }

        public bool OpenProject(string path)
        {
            _SGWorld.Project.Open(path, true);
            return true;
        }
    }

    /// <summary>
    /// 要素操作
    /// </summary>
    public class FeatureOperating : IFeatureOperating
    {
        public bool Create()
        {
            throw new NotImplementedException();
        }

        public bool Delete()
        {
            throw new NotImplementedException();
        }

        public void Modify()
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 拾取要素
    /// 临时要素不生效
    /// </summary>
    public class PickFeatureHandle : ISGWorldInteractionOperating
    {
        private readonly TerraExplorerX.ISGWorld701 _SGWorld;
        private Action<IGeometry> _Callback;
        private ITerraExplorerObject701 lastObject; //上一个Object
        private ITerraExplorerObject701 mouseOverObject; //鼠标经过Object

        public PickFeatureHandle(TerraExplorerX.ISGWorld701 sGWorld, Action<IGeometry> callback)
        {
            _SGWorld = sGWorld;
            _Callback = callback;
            _SGWorld.Window.SetInputMode(TerraExplorerX.MouseInputMode.MI_COM_CLIENT);
        }

        public void Frame()
        {
            var mi = _SGWorld.Window.GetMouseInfo();
            var pickObj = GetObject(mi.X, mi.Y);

            if ((mouseOverObject?.ID != pickObj?.ID && lastObject?.ID != pickObj?.ID) || pickObj.IsNullOrEmpty())
            {
                if (mouseOverObject is ITerrainPolygon701 polygon1)
                {
                    polygon1.FillStyle.Color.abgrColor = SGWorldConfig.DefaultFillColor;
                }
                else if (mouseOverObject is ITerrainPolyline701 line1)
                {
                    line1.LineStyle.Width = SGWorldConfig.DefaultLineWidth;
                    line1.LineStyle.Color.abgrColor = SGWorldConfig.DefaultLineColor;
                }

                if (pickObj is ITerrainPolygon701 polygon2)
                {
                    polygon2.FillStyle.Color.FromHTMLColor("#FF9800");
                }
                else if (pickObj is ITerrainPolyline701 line2)
                {
                    line2.LineStyle.Width = (SGWorldConfig.DefaultLineWidth * 2);
                    line2.LineStyle.Color.FromHTMLColor("#FF9800");
                }
            }

            //有效对象才更新
            if (pickObj.IsValuable()) mouseOverObject = pickObj;
        }

        public bool LeftButtonDown(int flags, int x, int y)
        {
            var pickObj = GetObject(x, y);

            if (lastObject?.ID != pickObj?.ID)
            {
                if (lastObject is ITerrainPolygon701 polygon1)
                {
                    polygon1.FillStyle.Color.abgrColor = SGWorldConfig.DefaultFillColor;
                }
                else if (lastObject is ITerrainPolyline701 line1)
                {
                    line1.LineStyle.BackColor.abgrColor = SGWorldConfig.DefaultLineColor;
                }

                if (pickObj is ITerrainPolygon701 polygon2)
                {
                    polygon2.FillStyle.Color.FromHTMLColor("#FF9800");
                }
                else if (pickObj is ITerrainPolyline701 line2)
                {
                    line2.LineStyle.Width = (SGWorldConfig.DefaultLineWidth * 2);
                    line2.LineStyle.Color.FromHTMLColor("#FF9800");
                }

                lastObject = pickObj;
            }

            return false;
        }

        private ITerraExplorerObject701 GetObject(int x, int y)
        {
            var pw = _SGWorld.Window.PixelToWorld(x, y);
            if (!string.IsNullOrWhiteSpace(pw?.ObjectID))
            {
                var pickObj = _SGWorld.ProjectTree.GetObject(pw.ObjectID);
                return pickObj;
            }
            return null;
        }

        private TInterfaces GetObject<TInterfaces>(int x, int y)
        {
            if (GetObject(x, y) is TInterfaces inst)
            {
                return inst;
            }
            return default(TInterfaces);
        }

        public bool RightButtonDown(int flags, int x, int y)
        {
            Dispose();
            return true;
        }

        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用

        public bool IsDispose { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    lastObject = null;
                    _Callback = null;
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        ~PickFeatureHandle()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(false);
        }

        // 添加此代码以正确实现可处置模式。
        public void Dispose()
        {
            //复原鼠标模式
            _SGWorld.Window.SetInputMode(0);
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器，则取消注释以下行。
            GC.SuppressFinalize(this);
            IsDispose = true;
        }
        #endregion
    }

    /// <summary>
    /// 点绘制处理
    /// </summary>
    public class PointDrawingHandle : IDrawingOperating, ISGWorldInteractionOperating
    {
        private readonly TerraExplorerX.ISGWorld701 _SGWorld;
        private TerraExplorerX.ITerrainImageLabel701 _ImageLabel;
        private Action<DrawResult, IDrawingOperating> _Callback;
        private List<string> temporaryTables = new List<string>();

        public PointDrawingHandle(TerraExplorerX.ISGWorld701 sGWorld, Action<DrawResult, IDrawingOperating> callback)
        {
            _SGWorld = sGWorld;
            _SGWorld.Window.SetInputMode(TerraExplorerX.MouseInputMode.MI_COM_CLIENT);
            //回调赋值
            _Callback = callback;
            //创建实例
            Create();
        }

        private void Create()
        {
            string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SGWorldConfig.DefaultMarkerIcon);
            //获取中心点坐标
            var pos = _SGWorld.Navigate.GetPosition(TerraExplorerX.AltitudeTypeCode.ATC_3DML_RELATIVE);
            //创建临时要素
            _ImageLabel = _SGWorld.Creator.CreateImageLabel(pos, filePath);
        }

        public void Remove()
        {
            //移除临时要素
            temporaryTables.TryForEach(t => _SGWorld.ProjectTree.DeleteItem(t));
            //恢复鼠标模式
            _SGWorld.Window.SetInputMode(0);
        }

        public void Frame()
        {
            if (_ImageLabel.IsValuable())
            {
                var mi = _SGWorld.Window.GetMouseInfo();
                var wpi = _SGWorld.Window.PixelToWorld(mi.X, mi.Y, TerraExplorerX.WorldPointType.WPT_TERRAIN);
                if (wpi.IsValuable()) _ImageLabel.Position = wpi.Position;
            }
        }

        public bool LeftButtonDown(int flags, int x, int y)
        {
            if (_ImageLabel.IsValuable())
            {
                var pos = _ImageLabel.Position;

                _Callback(new DrawResult(_ImageLabel.ID, pos.X, pos.Y, pos.Altitude), this);
                temporaryTables.Add(_ImageLabel.ID);
                //创建一个新的实例
                Create();
            }
            return false;
        }

        public bool RightButtonDown(int flags, int x, int y)
        {
            Dispose();
            return true;
        }

        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用

        public bool IsDispose { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    if (!temporaryTables.Any(a => _ImageLabel?.ID == a))
                    {
                        _SGWorld.ProjectTree.DeleteItem(_ImageLabel.ID);
                        _ImageLabel = null;
                        _Callback = null;
                        temporaryTables = null;
                    }
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        ~PointDrawingHandle()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(false);
        }

        // 添加此代码以正确实现可处置模式。
        public void Dispose()
        {
            //恢复鼠标模式
            _SGWorld.Window.SetInputMode(0);
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器，则取消注释以下行。
            GC.SuppressFinalize(this);
            IsDispose = true;
        }
        #endregion
    }

    /// <summary>
    /// 线绘制处理
    /// </summary>
    public class LineDrawingHandle : IDrawingOperating, ISGWorldInteractionOperating
    {
        private readonly TerraExplorerX.ISGWorld701 _SGWorld;
        private TerraExplorerX.ITerrainPolyline701 _Polyline;
        private Action<DrawResult, IDrawingOperating> _Callback;
        private List<string> temporaryTables = new List<string>();

        public LineDrawingHandle(TerraExplorerX.ISGWorld701 sGWorld, Action<DrawResult, IDrawingOperating> callback)
        {
            _SGWorld = sGWorld;
            _SGWorld.Window.SetInputMode(TerraExplorerX.MouseInputMode.MI_COM_CLIENT);

            //回调赋值
            _Callback = callback;
        }

        public void Remove()
        {
            //移除临时要素
            temporaryTables.TryForEach(t => _SGWorld.ProjectTree.DeleteItem(t));
            //恢复鼠标模式
            _SGWorld.Window.SetInputMode(0);
        }

        public void Frame()
        {
            var mi = _SGWorld.Window.GetMouseInfo();
            var wpi = _SGWorld.Window.PixelToWorld(mi.X, mi.Y, TerraExplorerX.WorldPointType.WPT_TERRAIN);
            if (wpi.IsValuable() && _Polyline?.Geometry is TerraExplorerX.ILineString line)
            {
                var pos = wpi.Position;
                var points = line.Points;
                int beforePointIndex = points.Count - 1;
                var editPoint = (points[beforePointIndex] as TerraExplorerX.IPoint);
                editPoint.X = pos.X;
                editPoint.Y = pos.Y;
                editPoint.Z = pos.Distance;
            }
        }

        public bool LeftButtonDown(int flags, int x, int y)
        {
            var wpi = _SGWorld.Window.PixelToWorld(x, y, TerraExplorerX.WorldPointType.WPT_TERRAIN);
            if (wpi.IsNullOrEmpty()) return false;
            var pos = wpi.Position;
            if (_Polyline.IsNullOrEmpty())
            {
                double[] verticesArray = new double[] { pos.X, pos.Y, 0, pos.X, pos.Y, 0 };
                TerraExplorerX.ILineString line = _SGWorld.Creator.GeometryCreator.CreateLineStringGeometry(verticesArray);
                _Polyline = _SGWorld.Creator.CreatePolyline(line, SGWorldConfig.DefaultLineColor);
                _Polyline.Geometry.StartEdit();
            }
            else
            {
                TerraExplorerX.ILineString line = _Polyline.Geometry as TerraExplorerX.ILineString;
                line.Points.AddPoint(pos.X, pos.Y, pos.Distance);
            }
            return false;
        }

        public bool RightButtonDown(int flags, int x, int y)
        {
            _Polyline?.Geometry.EndEdit();

            if (_Polyline?.Geometry.Wks.ExportToWKB(false) is byte[] wkb)
            {
                _Callback(new DrawResult(_Polyline.ID, wkb), this);
                temporaryTables.Add(_Polyline.ID);
            }
            //置为空
            _Polyline = null;
            return true;
        }

        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用

        public bool IsDispose { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    _Polyline = null;
                    _Callback = null;
                    temporaryTables = null;
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        ~LineDrawingHandle()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(false);
        }

        // 添加此代码以正确实现可处置模式。
        public void Dispose()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器，则取消注释以下行。
            GC.SuppressFinalize(this);
            IsDispose = true;
        }
        #endregion
    }

    /// <summary>
    /// 面绘制处理
    /// </summary>
    public class PolygonDrawingHandle : IDrawingOperating, ISGWorldInteractionOperating
    {
        private readonly TerraExplorerX.ISGWorld701 _SGWorld;
        private TerraExplorerX.ITerrainPolyline701 _Polyline;
        private TerraExplorerX.ITerrainPolygon701 _Polygon;
        private Action<DrawResult, IDrawingOperating> _Callback;
        private List<string> temporaryTables = new List<string>();

        public PolygonDrawingHandle(TerraExplorerX.ISGWorld701 sGWorld, Action<DrawResult, IDrawingOperating> callback)
        {
            _SGWorld = sGWorld;
            _SGWorld.Window.SetInputMode(TerraExplorerX.MouseInputMode.MI_COM_CLIENT);
            //回调赋值
            _Callback = callback;
        }

        public void Remove()
        {
            //移除临时要素
            temporaryTables.TryForEach(t => _SGWorld.ProjectTree.DeleteItem(t));
            //恢复鼠标模式
            _SGWorld.Window.SetInputMode(0);
        }

        public void Frame()
        {
            var mi = _SGWorld.Window.GetMouseInfo();
            var wpi = _SGWorld.Window.PixelToWorld(mi.X, mi.Y, TerraExplorerX.WorldPointType.WPT_TERRAIN);
            if (wpi.IsValuable())
            {
                var pos = wpi.Position;
                //更新几何图形
                if (_Polygon?.Geometry is TerraExplorerX.IPolygon polygon)
                {
                    foreach (TerraExplorerX.ILinearRing ring in polygon.Rings)
                    {
                        TerraExplorerX.IPoints points = ring.Points;
                        int beforePointIndex = points.Count - 1;
                        var editPoint = (points[beforePointIndex] as TerraExplorerX.IPoint);
                        editPoint.X = pos.X;
                        editPoint.Y = pos.Y;
                        editPoint.Z = pos.Distance;
                    }
                }
                else if (_Polyline?.Geometry is TerraExplorerX.ILineString line)
                {
                    var points = line.Points;
                    int beforePointIndex = points.Count - 1;
                    var editPoint = (points[beforePointIndex] as TerraExplorerX.IPoint);
                    editPoint.X = pos.X;
                    editPoint.Y = pos.Y;
                    editPoint.Z = pos.Distance;
                }
            }
        }

        public bool LeftButtonDown(int flags, int x, int y)
        {
            var wpi = _SGWorld.Window.PixelToWorld(x, y, TerraExplorerX.WorldPointType.WPT_TERRAIN);
            if (wpi.IsNullOrEmpty()) return false;
            var pos = wpi.Position;

            if (_Polyline.IsNullOrEmpty() && _Polygon.IsNullOrEmpty())
            {
                //初始化坐标
                double[] verticesArray = new double[] {
                    pos.X, pos.Y, pos.Distance,
                    pos.X, pos.Y, pos.Distance
                };
                //根据坐标创建初始图形 line
                TerraExplorerX.ILineString line = _SGWorld.Creator.GeometryCreator.CreateLineStringGeometry(verticesArray);
                _Polyline = _SGWorld.Creator.CreatePolyline(line);
                _Polyline.Geometry.StartEdit();
            }
            else
            {
                if (_Polyline?.Geometry is TerraExplorerX.ILineString line)
                {
                    //添加第三个点
                    List<double> verticesArray = (line.Points.ToArray() as object[]).Cast<double>().ToList();
                    verticesArray.InsertRange(3, new double[]
                    {
                        pos.X, pos.Y, pos.Distance,
                        pos.X, pos.Y, pos.Distance
                    });

                    //根据坐标创建面
                    TerraExplorerX.ILinearRing ring = _SGWorld.Creator.GeometryCreator.CreateLinearRingGeometry(verticesArray.ToArray());
                    _Polygon = _SGWorld.Creator.CreatePolygon(ring, FillColor: SGWorldConfig.DefaultFillColor);
                    _Polygon.Geometry.StartEdit();

                    //关闭辅助线
                    _Polyline.Geometry.EndEdit();
                    _SGWorld.ProjectTree.DeleteItem(_Polyline.ID);
                    _Polyline = null;
                }
                else if (_Polygon?.Geometry is TerraExplorerX.IPolygon polygon)
                {
                    foreach (TerraExplorerX.ILinearRing ring in polygon.Rings)
                    {
                        ring.Points.AddPoint(pos.X, pos.Y, pos.Distance);
                    }
                }
            }
            return false;
        }

        public bool RightButtonDown(int flags, int x, int y)
        {
            _Polygon?.Geometry.EndEdit();

            if (_Polygon?.Geometry.Wks.ExportToWKB(false) is byte[] wkb)
            {
                _Callback(new DrawResult(_Polygon.ID, wkb), this);
                temporaryTables.Add(_Polygon.ID);
            }
            //重置
            _Polygon = null;
            return true;
        }

        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用

        public bool IsDispose { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    _Polyline = null;
                    _Polygon = null;
                    _Callback = null;
                    temporaryTables = null;
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        ~PolygonDrawingHandle()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(false);
        }

        // 添加此代码以正确实现可处置模式。
        public void Dispose()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器，则取消注释以下行。
            GC.SuppressFinalize(this);
            IsDispose = true;
        }
        #endregion
    }
}
